<template>
  <base-breadcrumb :title="$route.meta.title">
    <!-- BIM人员取证台账编辑 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel != 0 ? rules : {}"
      id="base-breadcrumb"
      style="position: relative"
    >
      <a-spin :spinning="loading">
        <!-- 基本信息 -->
        <a-card :bordered="false" style="margin-bottom: 50px">
          <title-name title="基本信息"></title-name>
          <custom-form
            :isEmit="false"
            :formList="formList"
            :form="form"
            :editModel="editModel == 0 ? 'V' : ''"
            :formComponents="$refs"
          ></custom-form>
          <!-- 页脚操作栏 -->
          <div class="btn_box page-btn-right-top">
            <a-button :loading="loading" v-if="editModel != 0" type="primary" class="button_right" @click="handleSubmit"
              >提交</a-button
            >
            <a-button v-if="checkVisibleInDetail" type="primary" @click="handleActiveClose">关闭</a-button>
          </div>
        </a-card>
      </a-spin>
    </a-form-model>
  </base-breadcrumb>
</template>

<script>
import {
  insertBimPersonnelObtainEvidenceLedger,
  updateBimPersonnelObtainEvidenceLedger,
  getBimPersonnelObtainEvidenceLedgerById,
  getPageTypesLabel
} from '@/api/bimIntegrate/bimResource.js'
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import { createValidator, isPhone } from '@/utils/regExpression' // 正则校验
import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject'
import Selector from '@/components/Select/Selector'

export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  data() {
    return {
      loading: false, // 提交按钮loding
      editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
      id: '', // 业务id
      form: {
        unitName: '', // 单位
        unitId: '' // 单位id
      }, // 表单
      rules: {
        unitId: [{ required: true, message: '请选择单位', trigger: 'change' }],
        userId: [{ required: true, message: '请选择人员', trigger: 'change' }],
        phone: [{ required: true, validator: createValidator(isPhone, '请输入正确的手机号码', true), trigger: 'blur' }],
        certifiedAgencyId: [{ required: true, message: '请选择颁证机构', trigger: 'change' }],
        certificateGradeId: [{ required: true, message: '请选择证书等级', trigger: 'change' }],
        category: [{ required: true, message: '请选择类别', trigger: 'change' }],
        evidenceTime: [{ required: true, message: '请选择取证时间', trigger: 'change' }]
        // fileInfoList: [{ type: 'array', required: true, message: '请上传文件', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.$route.meta.title = ['人员取证台账查看', '人员取证台账编辑', '人员取证台账新增'][this.editModel]
    // 不为添加时请求详情
    if (this.editModel != 2) {
      this.loading = true
      getBimPersonnelObtainEvidenceLedgerById({
        id: this.id
      })
        .then((res) => {
          this.loading = false
          this.form = res.data
        })
        .catch(() => {
          this.loading = false
        })
    } else {
      this.form.unitName = this.$currentPost()?.name // 默认单位
      this.form.unitId = this.$currentPost()?.id // 默认单位id
    }
  },
  computed: {
    // 表单列表
    formList() {
      return [
        {
          keyName: 'unitId',
          viewName: 'unitName',
          type: SelectCompanyAndProject,
          label: '单位',
          attrs: {
            extraParams: {
              notContainTags: ['PD']
            }
          },
          onSelectChange: (e) => {
            this.form.unitName = e.name
          }
        },
        {
          keyName: 'userId',
          type: 'SelectPerson',
          label: '姓名',
          personKeys: ['userId', 'userName'],
          isSingle: true,
          attrs: {
            // isMutilple: false
          },
          confirmSelect: (e) => {
            this.$set(this.form, 'deptName', e?.postPNames?.join())
            this.$set(this.form, 'employeeNum', e?.jobNum)
            this.$set(this.form, 'phone', e?.phone)
          }
        },
        {
          keyName: 'deptName',
          type: 'BaseInput',
          placeholder: '请选择姓名',
          disabled: true,
          label: '部门/项目'
        },
        {
          keyName: 'employeeNum',
          placeholder: '请选择姓名',
          type: 'BaseInput',
          disabled: true,
          label: '员工编号'
        },
        {
          keyName: 'category',
          type: 'a-select',
          label: '类别',
          data: [
            {
              label: '信息化',
              value: 0
            },
            {
              label: 'BIM',
              value: 1
            }
          ],
          customRender(e) {
            return ['信息化', 'BIM'][e] || '无'
          },
          change: (e) => {
            // 映射类别name
            this.form.categoryName = ['信息化', 'BIM'][e]
          }
        },
        {
          keyName: 'phone',
          type: 'BaseInput',
          label: '联系方式'
        },
        {
          keyName: 'certifiedAgencyId',
          type: Selector,
          attrs: {
            customApi: getPageTypesLabel,
            customQuery: { param: { type: '40' } },
            dataValue: 'data.list'
          },
          label: '颁证机构',
          getOption: (e) => {
            // 映射奖项名称name
            this.form.certifiedAgency = e?.name
          }
        },
        {
          keyName: 'certificateGradeId',
          type: Selector,
          attrs: {
            customApi: getPageTypesLabel,
            customQuery: { param: { type: '41' } },
            dataValue: 'data.list'
          },
          label: '证书等级',
          getOption: (e) => {
            // 映射奖项名称name
            this.form.certificateGrade = e?.name
          }
        },
        {
          keyName: 'evidenceTime',
          type: 'a-date-picker',
          label: '取证时间',
          attrs: {
            valueFormat: 'YYYY-MM-DD'
          }
        },
        {
          keyName: 'fileInfoList',
          type: 'uploadFile',
          label: '附件',
          span: 24,
          businessCode: 'BIM03'
        }
      ]
    }
  },
  methods: {
    /**
     * 提交表单
     */
    handleSubmit() {
      let that = this
      let url = [updateBimPersonnelObtainEvidenceLedger, insertBimPersonnelObtainEvidenceLedger][that.editModel - 1] // 根据操作切换url地址
      // 校验表单
      that.$refs.ruleForm.validate((e) => {
        if (e) {
          // 提交表单
          that.loading = true
          console.log(url, that.form)
          let form = that.$clone(that.form)
          url(form)
            .then((res) => {
              that.loading = false
              if (res.code === '0' || res.data.code === '0') {
                that.$message.success('提交成功！')
                that.$multiTab.close()
              }
            })
            .catch(() => {
              that.loading = false
            })
        }
      })
    }
  }
}
</script>